package com.unidt.www.module.third.enterprise.service.impl;


import com.unidt.www.framework.common.pojo.PageResult;
import com.unidt.www.framework.common.util.object.BeanNullUtil;
import com.unidt.www.module.third.enterprise.dao.EnterpriseNoticeMapper;
import com.unidt.www.module.third.enterprise.dao.EnterpriseUserMapper;
import com.unidt.www.module.third.enterprise.domain.EnterpriseNoticeDO;
import com.unidt.www.module.third.enterprise.domain.EnterpriseUserDO;
import com.unidt.www.module.third.enterprise.service.EnterpriseUserService;
import com.unidt.www.module.third.enterprise.vo.user.EnterpriseUserCreatReqVO;
import com.unidt.www.module.third.enterprise.vo.user.EnterpriseUserFollowReqVO;
import com.unidt.www.module.third.enterprise.vo.user.EnterpriseUserListReqVO;
import com.unidt.www.module.third.enterprise.vo.user.EnterpriseUserUpdateReqVO;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.unidt.www.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author ouyangoyys
 * @since 2022-11-24
 */
@Service
@Log4j2
public class EnterpriseUserServiceImpl implements EnterpriseUserService {


    @Resource
    private EnterpriseUserMapper enterpriseUserMapper;

    @Resource
    private EnterpriseNoticeMapper enterprisemapper;

    @Override
    public Long create(EnterpriseUserCreatReqVO reqVO) {

        reqVO.setUid(getLoginUserId());
        EnterpriseUserDO mEnterpriseUserDO=new EnterpriseUserDO();
        BeanUtils.copyProperties(reqVO,mEnterpriseUserDO);




        enterpriseUserMapper.insert(mEnterpriseUserDO);

        return mEnterpriseUserDO.getId();
    }
    @Override
    public Long follow(EnterpriseUserFollowReqVO reqVO) {
        reqVO.setUid(getLoginUserId());
        if(reqVO.getFollowStatus()==1) {
            /**
             * 判空，解决重复关注问题。
             */
            EnterpriseUserDO mSelectEnterpriseUser=enterpriseUserMapper.selectEnterpriseUserDO(reqVO);
            if(mSelectEnterpriseUser==null) {
                EnterpriseUserDO mEnterpriseUserDO = new EnterpriseUserDO();
                BeanUtils.copyProperties(reqVO, mEnterpriseUserDO);
                enterpriseUserMapper.insert(mEnterpriseUserDO);
            }
            return 1L;


        }else if(reqVO.getFollowStatus()==2){

            EnterpriseUserDO mEnterpriseUserDO =enterpriseUserMapper.selectEnterpriseUserDO(reqVO);
            if(mEnterpriseUserDO!=null){
                enterpriseUserMapper.deleteById(mEnterpriseUserDO.getId());
            }
            return 2L;
        }
        return 0L;

    }
    @Override
    public void update(EnterpriseUserUpdateReqVO reqVO) {
        reqVO.setUid(getLoginUserId());
        EnterpriseUserDO mEnterpriseUserDO =enterpriseUserMapper.selectById(reqVO.getId());
        if(mEnterpriseUserDO!=null){
            BeanUtils.copyProperties(reqVO, mEnterpriseUserDO, BeanNullUtil.getNullPropertyNames(reqVO));
            enterpriseUserMapper.updateById(mEnterpriseUserDO);
        }

    }

    @Override
    public void delete(Long id) {
        enterpriseUserMapper.deleteById(id);
    }

    @Override
    public   PageResult<EnterpriseNoticeDO> list(EnterpriseUserListReqVO reqVO) {
        reqVO.setUid(getLoginUserId());
        PageResult<EnterpriseUserDO> mPageResult=enterpriseUserMapper.selectList(reqVO);

        if(mPageResult.getList()==null){
            return null;
        }
        List<EnterpriseNoticeDO> mList=new ArrayList<>();

        PageResult<EnterpriseNoticeDO> mResult=new PageResult<EnterpriseNoticeDO> ();

        for(EnterpriseUserDO mEnterpriseUserDO: mPageResult.getList()){

            EnterpriseNoticeDO mEnterpriseDO=enterprisemapper.selectById(mEnterpriseUserDO.getEnterpriseId());

            mList.add(mEnterpriseDO);
        }

        mResult.setList(mList);
        mResult.setTotal(mPageResult.getTotal());
        return mResult;
    }

}
